﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-21>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：抄表数据录入
//********************************************************************************//

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ChaoBiaoGL
{
    public partial class ChaoBiaoSJLR_DXS : BasePage
    {
        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtData
        {
            get { return ViewState["dtData"] as DataTable; }
            set { ViewState["dtData"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _spHelper = new SpHelper();

        /// <summary>
        ///     用户信息BLL
        /// </summary>
        private readonly QS_YG_YONGHUXXBLL _YHXXBLL = new QS_YG_YONGHUXXBLL();

        /// <summary>
        ///     深井信息BLL
        /// </summary>
        private readonly QS_YH_SHENJINGXXBBLL _SJXXBLL = new QS_YH_SHENJINGXXBBLL();

        /// <summary>
        ///     取水口信息bll
        /// </summary>
        private readonly QS_YH_QUSHUIKXXBBLL _QSKBLL = new QS_YH_QUSHUIKXXBBLL();

        /// <summary>
        ///     水表信息bll
        /// </summary>
        private readonly QS_YH_SHUIBIAOXXBBLL _SBXXBLL = new QS_YH_SHUIBIAOXXBBLL();

        /// <summary>
        ///     抄表数据BLL
        /// </summary>
        private QS_CB_CHAOBIAOSJ _CBSJ = new QS_CB_CHAOBIAOSJ();

        /// <summary>
        ///     抄表数据BLL
        /// </summary>
        private readonly QS_CB_CHAOBIAOSJBLL _CBSJBLL = new QS_CB_CHAOBIAOSJBLL();

        /// <summary>
        ///     日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                LoadData();
                Loadzs();
                LoadDataTxt(1);
                Display();
                // FocusChange();
            }

            gvList.DataSource = dtData;
            gvList.DataBind();
        }

        /// <summary>
        ///     修改操作
        /// </summary>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Status = GridViewStatus.Update;
            Display();
        }


        /// <summary>
        ///     保存验证
        /// </summary>
        /// <returns></returns>
        private string ValidateData()
        {
            var mg = "0";
            if (!PageTools.ValidateString(txtBenYueCH.Text, PageTools.eValidateType.数字))
            {
                mg = "1";
            }
            else if (!PageTools.ValidateString(txtBenYueSL.Text, PageTools.eValidateType.数字))
            {
                mg = "2";
            }

            else if (txtChaoBiaoZT.Text == "")
            {
                mg = "4";
            }
            else if (Convert.ToInt32(txtBenYueCH.Text) - Convert.ToInt32(txtShangYueCM.Text) !=
                     Convert.ToInt32(txtBenYueSL.Text))
            {
                //本月抄码-上月抄码=本月水量
                if (txtChaoBiaoZT.Text == "0")
                    mg = "5";
                else
                    mg = "0";
            }
            else
            {
                //验证该水表是否为此用户的有效水表
                var dt_sb = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(string.Format(
                    @"i_jlzt=0 and s_shuibiaobh='{1}' and S_QUSHUIK='{2}' 
 and exists (select 1 from qs_yh_yonghusbgx where s_shuibiaoh='{1}' and s_yonghuh='{0}' and i_jlzt=0)",
                    txtYongHuH.Text, txtShuiBiaoBH.Text, txtJingBH.Text), null);
                if (dt_sb.Rows.Count < 1)
                    mg = "3";
                else
                    mg = "0";
            }

            return mg;
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = 2; // BusinessTool.WType;
            span1.InnerText = "井编号：";
            //txtKaiZhangDL.Visible = BusinessTool.WType == 1 ? true : false;
            //水量年月
            var ym = BusinessTool.GetSLYMorZWNY(1, wtype);
            txtShuiLiangYM.Text = ym.ToString();
            LoadCH();
            txtYongHuH.Focus();
            var dt = _spHelper.P_QSCB_YZSFKZ(wtype, ym).Tables[0];
            if (dt != null && dt.Rows.Count > 0 && Convert.ToInt32(dt.Rows[0][0]) > 0)
            {
                btnSave.ClientEnabled = false;
                btnCancel.ClientEnabled = false;
                btnHQSJ.ClientEnabled = false;
                UpLoadFile.ClientEnabled = false;
            }
        }

        /// <summary>
        ///     加载册号
        /// </summary>
        private void LoadCH()
        {
            string[] arr =
            {
                "001", "003", "005", "008", "009", "101", "106", "111", "113", "115", "204", "207", "208", "209", "210",
                "213", "214", "216", "217", "218"
            };

            for (var i = 0; i < arr.Length; i++) cbmCeHao.Items.Add(arr[i], arr[i]);
        }

        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
            var wtype = BusinessTool.WType;
            dtData = _spHelper.P_QSCB_CHAOBIAOSJ(wtype, 0).Tables[0];
            gvList.DataSource = dtData;
            gvList.DataBind();
            if (dtData != null && dtData.Rows.Count > 0) zjl.Text = "共" + dtData.Rows.Count + "条";
        }

        /// <summary>
        ///     用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (Status)
            {
                case GridViewStatus.Init:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = true;
                    //SetText(false);
                    break;
                case GridViewStatus.Search:
                    //SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = false;
                    //SetText(true);
                    break;
                case GridViewStatus.Add:
                    btnSave.Visible = true;
                    btnCancel.Visible = true;
                    //this.treeChaoBiaoSH.Enabled = false;
                    //SetText(true);
                    InitData();
                    break;
            }
        }


        ///// <summary>
        ///// 保存数据
        ///// </summary>
        //private void SaveData()
        //{
        //    int ym = Convert.ToInt32(txtShuiLiangYM.Text);
        //    int i_y = ym / 100;
        //    int i_m = ym % 100;
        //    OperateState operateState;
        //    int id = 0;
        //    DataTable dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and  i_y={3} and i_m={4}", txtYongHuH.Text, txtShuiBiaoBH.Text, txtJingBH.Text, i_y, i_m), null);
        //    if (dt_cb.Rows.Count > 0)
        //    {
        //        id = Convert.ToInt32(dt_cb.Rows[0]["ID"]);
        //        //修改操作
        //        operateState = OperateState.Modify;
        //        this._CBSJ = this._CBSJBLL.SelectQS_CB_CHAOBIAOSJ(id);
        //        this._CBSJ.ID = id;
        //    }
        //    //添加
        //    else
        //    {
        //        operateState = OperateState.Add;
        //        this._CBSJ.ID = 0;
        //        this._CBSJ.S_SHUIBIAOBH = txtShuiBiaoBH.Text;
        //        this._CBSJ.I_Y = i_y;
        //        this._CBSJ.I_M = i_m;
        //        this._CBSJ.I_JIHUASL = Convert.ToDecimal(txtBenYueSL.Text);
        //        this._CBSJ.S_YONGHUH = txtYongHuH.Text;
        //        this._CBSJ.S_JINGBH = txtJingBH.Text;
        //        this._CBSJ.S_CEHAO = cbmCeHao.Text;
        //        this._CBSJ.S_SHUILIANGLB = txtShuiLiangLB.Text;
        //        this._CBSJ.S_TIANJIAR = LoginInfo.CurrentUser.Account;
        //        this._CBSJ.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
        //        this._CBSJ.N_SHUIJIA = Convert.ToDecimal(txtShuiJia.Text);
        //    }

        //    this._CBSJ.I_JLZT = 0;
        //    this._CBSJ.I_CHAOBIAOZT = Convert.ToDecimal(txtChaoBiaoZT.Text);
        //    this._CBSJ.I_BENYUECM = Convert.ToDecimal(txtBenYueCH.Text);
        //    this._CBSJ.I_BENYUESL = Convert.ToDecimal(txtBenYueSL.Text);
        //    this._CBSJ.S_CAOZUOR = LoginInfo.CurrentUser.Account;
        //    this._CBSJ.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();

        //    //同时修改水表信息表里面的册号
        //    string res = "";
        //    _spHelper.P_QSCB_XGCH(BusinessTool.WType, txtShuiLiangLB.Text, cbmCeHao.Text, out res);

        //    _SYS_LOGBLL.Log<QS_CB_CHAOBIAOSJ>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "抄表数据录入", operateState, _CBSJ, null);
        //    bool result = this._CBSJBLL.InsertOrUpdate(_CBSJ);
        //    //执行操作
        //    if (result)
        //    {

        //        PageTools.AlertMsg(this, "保存成功！");
        //        LoadData();
        //        this.Status = GridViewStatus.Init;
        //        Display();
        //        //设置选中当前操作的项
        //        //             this.treeChaoBiaoSH.FindNodeByKeyValue(this._CBSJ.S_YONGHUH).Focus();
        //        //更新当前操作项ID
        //        //this.GuanLiKuaiID = Convert.ToInt32(((System.Data.DataRowView)(treeChaoBiaoSH.FocusedNode.DataItem)).Row.ItemArray[0].ToString().Split('_')[0]);
        //    }
        //    else
        //    {
        //        PageTools.AlertMsg(this, "保存失败！");
        //    }
        //}

        /// <summary>
        ///     给输入框赋值
        /// </summary>
        private void SetData(DataTable dt)
        {
            var y = Convert.ToInt32(txtShuiLiangYM.Text) / 100;
            var m = Convert.ToInt32(txtShuiLiangYM.Text) % 100;
            var yhh = dt.Rows[0]["S_YONGHUH"].ToString();
            var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
            var bh = dt.Rows[0]["BH"].ToString();
            var dz = dt.Rows[0]["DZ"].ToString();
            var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format(
                    "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                    yhh, sbh, bh, y, m), null);
            var y_sy = 0;
            var m_sy = 0;
            if (m == 1)
            {
                m_sy = 12;
                y_sy = y - 1;
            }
            else
            {
                m_sy = m - 1;
                y_sy = y;
            }

            var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format(
                    "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                    yhh, sbh, bh, y_sy, m_sy), null);

            txtJingBH.Text = bh;
            txtQuShuiDD.Text = dz;
            txtYongHuH.Text = yhh;
            txtYongHuM.Text = dt.Rows[0]["S_YONGHUM"].ToString();
            txtShuiJia.Text = dt.Rows[0]["N_SHUIJIA"].ToString();
            cbmCeHao.Text = dt.Rows[0]["S_CEHAO"].ToString();
            txtShuiBiaoBH.Text = sbh;
            var sllb = dt.Rows[0]["S_SHUILIANGLB"].ToString();
            if (sllb == "1")
                txtShuiLiangLB.Text = "普通采";
            else if (sllb == "2")
                txtShuiLiangLB.Text = "采灌采";
            else if (sllb == "3")
                txtShuiLiangLB.Text = "采灌灌";
            else if (sllb == "4")
                txtShuiLiangLB.Text = "矿泉采";
            else if (sllb == "5") txtShuiLiangLB.Text = "专灌";
            txtChaoBiaoZT.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() : "0";
            txtBenYueCH.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_BENYUECM"].ToString() : "";
            txtBenYueSL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_BENYUESL"].ToString() : "";
            txtShangYueCM.Text = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "";
            txtShangYueSL.Text = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "";
            //txtKaiZhangSL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_JIHUASL"].ToString() : "";
            //txtBenYueDL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_SHUILIANGDL"].ToString() : "";
            //txtKaiZhangDL.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["I_JIHUASLDL"].ToString() : "";
            ////cbmCeHao.Text = dt_cb.Rows.Count > 0 ? dt_cb.Rows[0]["S_CEHAO"].ToString() : "";
            //txtBL.Text = GetBL(yhh);
            if (txtChaoBiaoZT.Text == "") txtChaoBiaoZT.Text = "0";

            txtBL.Text = GetBL(yhh);

            txtBenYueCH.Focus();
        }


        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var CH = cbmCeHao.Text;
            var wtype = 2; // BusinessTool.WType;
            var y = Convert.ToInt32(txtShuiLiangYM.Text) / 100;
            var m = Convert.ToInt32(txtShuiLiangYM.Text) % 100;
            var y_sy = 0;
            var m_sy = 0;
            if (m == 1)
            {
                m_sy = 12;
                y_sy = y - 1;
            }
            else
            {
                m_sy = m - 1;
                y_sy = y;
            }

            var msg = "";
            if (e.Parameter == "info")
            {
                var yhh = hdCollection.Get("yhh").ToString();
                var bl = GetBL(yhh);
                var dt_yh = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("i_jlzt=0 and s_yonghuh='{0}'", yhh),
                    null);
                if (dt_yh.Rows.Count < 1)
                {
                    msg = "0";
                }
                else
                {
                    var dt_sj = wtype == 2
                        ? _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(
                            string.Format(
                                "S_JINGBH in (select S_JINGBIANH from QS_YH_YONGHUSJGXB where i_jlzt=0 and S_YONGHUBH='{0}') and i_jlzt=0",
                                yhh), null)
                        : null;
                    var dt_qsk = wtype == 1
                        ? _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(
                            string.Format(
                                "S_QUSHUIKBH in (select S_QUSHUIKH from QS_YH_YONGHUQSKGX where i_jlzt=0 and S_YONGHUH='{0}') and i_jlzt=0",
                                yhh), null)
                        : null;

                    var dt_sb = _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(
                        string.Format(
                            "S_SHUIBIAOBH in (select S_SHUIBIAOH from QS_YH_YONGHUSBGX where i_jlzt=0 and  S_YONGHUH='{0}') and i_jlzt=0",
                            yhh), null);

                    var bh = wtype == 2
                        ? dt_sj.Rows[0]["S_JINGBH"].ToString()
                        : dt_qsk.Rows[0]["S_QUSHUIKBH"].ToString();
                    var dz = wtype == 2
                        ? dt_sj.Rows[0]["S_JINGDIZ"].ToString()
                        : dt_qsk.Rows[0]["S_QUSHUIDZ"].ToString();
                    var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format(
                            "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                            yhh, dt_sb.Rows[0]["S_SHUIBIAOBH"], bh, y, m), null);

                    var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and i_y={2} and i_m={3}",
                            yhh, dt_sb.Rows[0]["S_SHUIBIAOBH"], y_sy, m_sy), null);
                    var sllb = getSllb(dt_sb.Rows[0]["S_SHUILIANGLB"].ToString());

                    if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count > 0)
                    {
                        var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                            ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                            : "0";
                        msg = string.Join("|", dt_yh.Rows[0]["S_YONGHUM"].ToString(),
                            dt_yh.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                            dt_sb.Rows[0]["S_CEHAO"].ToString(), dt_sb.Rows[0]["S_SHUIBIAOBH"].ToString(), sllb,
                            cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                            dt_cb_sy.Rows[0]["I_BENYUECM"].ToString(), dt_cb_sy.Rows[0]["I_BENYUESL"].ToString(),
                            dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                            dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl
                        );
                    }
                    else
                    {
                        var cm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "";
                        var sl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "";

                        msg = string.Join("|", dt_yh.Rows[0]["S_YONGHUM"].ToString(),
                            dt_yh.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                            dt_sb.Rows[0]["S_CEHAO"].ToString(), dt_sb.Rows[0]["S_SHUIBIAOBH"].ToString(), sllb,
                            "", "", "", cm_sy, sl_sy, "", "", "", yhh, bl);
                    }
                }
            }
            //  this.treeChaoBiaoSH.FindNodeByKeyValue(yhh).Focus();
            else if (e.Parameter == "setinfo")
            {
                var id = Convert.ToInt32(hdCollection.Get("ID"));
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ(wtype, id).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    var yhh2 = dt.Rows[0]["S_YONGHUH"].ToString();
                    var bl = GetBL(yhh2);
                    var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
                    //井编号或者取水口编号

                    var QSKorJBH =
                        _SBXXBLL.SelectQS_YH_SHUIBIAOXXBsDynamic(
                                string.Format("I_JLZT=0 AND I_WTYPE={0} AND S_SHUIBIAOBH='{1}'", wtype, sbh), null)
                            .Rows[0]["S_QUSHUIK"].ToString();
                    var dt_sj = wtype == 2
                        ? _SJXXBLL.SelectQS_YH_SHENJINGXXBsDynamic(
                            string.Format("S_JINGBH='{0}' and i_jlzt=0", QSKorJBH), null)
                        : null;
                    var dt_qsk = wtype == 1
                        ? _QSKBLL.SelectQS_YH_QUSHUIKXXBsDynamic(
                            string.Format("S_QUSHUIKBH='{0}' and i_jlzt=0", QSKorJBH), null)
                        : null;
                    var bh = wtype == 2
                        ? dt_sj.Rows[0]["S_JINGBH"].ToString()
                        : dt_qsk.Rows[0]["S_QUSHUIKBH"].ToString();
                    var dz = wtype == 2
                        ? dt_sj.Rows[0]["S_JINGDIZ"].ToString()
                        : dt_qsk.Rows[0]["S_QUSHUIDZ"].ToString();

                    var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format("s_yonghuh='{0}' and s_shuibiaobh='{1}'  and i_jlzt=0 and i_y={2} and i_m={3}",
                            yhh2, sbh, y_sy, m_sy), null);
                    var sllb = getSllb(dt.Rows[0]["S_SHUILIANGLB"].ToString());
                    var cbzt = dt.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                        ? dt.Rows[0]["I_CHAOBIAOZT"].ToString()
                        : "0";
                    var cm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "0";
                    var sl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "0";
                    msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh,
                        dz,
                        dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb,
                        cbzt, dt.Rows[0]["I_BENYUECM"].ToString(), dt.Rows[0]["I_BENYUESL"].ToString(),
                        cm_sy, sl_sy,
                        dt.Rows[0]["I_JIHUASL"].ToString(), dt.Rows[0]["I_SHUILIANGDL"].ToString(),
                        dt.Rows[0]["I_JIHUASLDL"].ToString(), yhh2, bl
                    );
                }
            }
            else if (e.Parameter == "change")
            {
                var ch = hdCollection.Get("ch").ToString();
                var dt1 = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 0, CH).Tables[0];
                var rs = dt1.Rows.Count > 0 ? Convert.ToInt32(dt1.Rows[0][0]) : 0;
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 1, CH).Tables[0];
                rows.Value = rs.ToString();
                if (dt.Rows.Count > 0) msg = SetText(dt, y, m, y_sy, m_sy);
                msg = msg + "|" + rs;
            }
            else if (e.Parameter == "first")
            {
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 1, CH).Tables[0];
                if (dt.Rows.Count > 0) msg = SetText(dt, y, m, y_sy, m_sy);
            }
            else if (e.Parameter == "last" || e.Parameter == "next" || e.Parameter == "final")
            {
                var rn = Convert.ToInt32(hdCollection.Get("RN"));
                var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, rn, CH).Tables[0];
                if (dt.Rows.Count > 0) msg = SetText(dt, y, m, y_sy, m_sy);
            }
            else if (e.Parameter == "hqsj")
            {
                var dt = _spHelper.P_QSCB_DRSJ(LoginInfo.CurrentUser.UserID, 2).Tables[0];
                msg = dt.Rows[0][0].ToString();
            }
            else if (e.Parameter == "save")
            {
                msg = ValidateData();
                if (msg == "0") //保存数据
                {
                    OperateState operateState;
                    operateState = OperateState.Modify;
                    var id = 0;
                    var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                        string.Format(
                            "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and  i_y={3} and i_m={4}",
                            txtYongHuH.Text, txtShuiBiaoBH.Text, txtJingBH.Text, y, m), null);

                    if (dt_cb.Rows.Count > 0)
                    {
                        id = Convert.ToInt32(dt_cb.Rows[0]["ID"]);
                        //修改操作
                        operateState = OperateState.Modify;
                        _CBSJ = _CBSJBLL.SelectQS_CB_CHAOBIAOSJ(id);
                        _CBSJ.ID = id;
                    }
                    //添加
                    else
                    {
                        operateState = OperateState.Add;
                        _CBSJ.ID = 0;
                        _CBSJ.S_SHUIBIAOBH = txtShuiBiaoBH.Text;
                        _CBSJ.I_Y = y;
                        _CBSJ.I_M = m;

                        _CBSJ.S_YONGHUH = txtYongHuH.Text;
                        _CBSJ.S_JINGBH = txtJingBH.Text;
                        var sjbll = new QS_YH_SHENJINGXXBBLL();
                        var dt_j = sjbll.SelectQS_YH_SHENJINGXXBsDynamic(
                            string.Format(" i_jlzt=0 and S_JINGBH='{0}'", txtJingBH.Text), null);
                        var dt_yh = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(
                            string.Format("i_jlzt=0 and i_wtype=2 and s_yonghuh='{0}'", txtYongHuH.Text), null);
                        var ceng = dt_j.Rows.Count > 0 ? Convert.ToInt32(dt_j.Rows[0]["I_CENG"]) : 0;
                        var bmbh = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_BUMENBH"].ToString() : "";
                        var qsyt = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_QUSHUIYTBH"].ToString() : "";
                        var hybh = dt_yh.Rows.Count > 0 ? dt_yh.Rows[0]["S_HANGYEBH"].ToString() : "";
                        var xzq = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_XINGZHENGQBH"].ToString() : "";
                        var lybh = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_LIUYUBH"].ToString() : "";
                        var hxbj = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_HUANGXIANBH"].ToString() : "";
                        var xkz = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_XUKEZHENGBH"].ToString() : "";
                        var jlxbh = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_JINGLEIXBH"].ToString() : "";
                        var jbsbh = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_JINGBIAOSBH"].ToString() : "";
                        var jztbh = dt_j.Rows.Count > 0 ? dt_j.Rows[0]["S_JINGZHUANGTBH"].ToString() : "";
                        _CBSJ.S_BUMENBH = bmbh;
                        _CBSJ.S_QUSHUIYTBH = qsyt;
                        _CBSJ.S_HANGYEBH = hybh;
                        _CBSJ.S_XINGZHENGQBH = xzq;
                        _CBSJ.S_LIUYUBH = lybh;
                        _CBSJ.S_HUANXIANBJ = hxbj;
                        _CBSJ.S_XUKEZBH = xkz;
                        _CBSJ.S_JINGLEIXBH = jlxbh;
                        _CBSJ.S_JINGBIAOSBH = jbsbh;
                        _CBSJ.S_JINGZHUANGTBH = jztbh;
                        _CBSJ.I_CENG = ceng;
                        _CBSJ.S_CEHAO = cbmCeHao.Text;
                        var sllb = getSllb2(txtShuiLiangLB.Text.Trim());
                        _CBSJ.S_SHUILIANGLB = sllb;
                        _CBSJ.S_TIANJIAR = LoginInfo.CurrentUser.UserID.ToString();
                        _CBSJ.D_TIANJIARQ = BusinessTool.GetDataBaseDateTime();
                        _CBSJ.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                        _CBSJ.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                        _CBSJ.N_SHUIJIA = Convert.ToDecimal(txtShuiJia.Text);
                        //PageTools.AlertMsg(this,"抄表数据不存在，无法修改");
                        //return;
                    }

                    _CBSJ.I_JLZT = 0;
                    _CBSJ.I_CHAOBIAOZT = Convert.ToDecimal(txtChaoBiaoZT.Text);
                    _CBSJ.I_BENYUECM = Convert.ToDecimal(txtBenYueCH.Text);
                    _CBSJ.I_BENYUESL = Convert.ToDecimal(txtBenYueSL.Text);
                    var bl = txtBL.Text != "" ? Convert.ToInt32(txtBL.Text) : 100;
                    if (txtChaoBiaoZT.Text == "2" || txtChaoBiaoZT.Text == "3")
                    {
                        var sl = txtBenYueSL2.Text == ""
                            ? Convert.ToDecimal(txtBenYueSL.Text)
                            : Convert.ToDecimal(txtBenYueSL2.Text); //* (Convert.ToDecimal(txtBL.Text) / 1000);//开帐水量
                        _CBSJ.I_JIHUASL = sl * bl / 100;
                    }
                    else
                    {
                        _CBSJ.I_JIHUASL = Convert.ToDecimal(txtBenYueSL.Text) * bl / 100; //跟百分比水量计算开账水量
                    }

                    _CBSJ.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                    _CBSJ.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                    // this._CBSJ.I_SHUILIANGDL = txtBenYueDL.Text == "" ? 0 : Convert.ToDecimal(txtBenYueDL.Text);

                    //同时修改水表信息表里面的册号
                    var res = "";
                    _spHelper.P_QSCB_XGCH(wtype, txtShuiLiangLB.Text, cbmCeHao.Text, out res);

                    _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "抄表数据录入", operateState, _CBSJ,
                        null);
                    var result = _CBSJBLL.InsertOrUpdate(_CBSJ);
                    //执行操作
                    if (result)
                    {
                        Status = GridViewStatus.Init;
                        msg = "t";
                    }
                }
            }

            e.Result = e.Parameter + "|0|" + msg;
        }

        /// <summary>
        ///     返回字符串
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="y"></param>
        /// <param name="m"></param>
        /// <param name="y_sy"></param>
        /// <param name="m_sy"></param>
        /// <returns></returns>
        private string SetText(DataTable dt, int y, int m, int y_sy, int m_sy)
        {
            var msg = "";
            var yhh = dt.Rows[0]["S_YONGHUH"].ToString();
            var sbh = dt.Rows[0]["S_SHUIBIAOBH"].ToString();
            var bh = dt.Rows[0]["BH"].ToString();
            var dz = dt.Rows[0]["DZ"].ToString();
            var bl = GetBL(yhh);

            var dt_cb = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format(
                    "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                    yhh, sbh, bh, y, m), null);
            var dt_cb_sy = _CBSJBLL.SelectQS_CB_CHAOBIAOSJsDynamic(
                string.Format(
                    "s_yonghuh='{0}' and s_shuibiaobh='{1}' and s_jingbh='{2}' and i_jlzt=0 and i_y={3} and i_m={4}",
                    yhh, sbh, bh, y_sy, m_sy), null);
            var sllb = dt.Rows[0]["S_SHUILIANGLB"].ToString();
            var sllb2 = getSllb(sllb);
            if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count > 0)
            {
                var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                    ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                    : "0";
                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2,
                    cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                    dt_cb_sy.Rows[0]["I_BENYUECM"].ToString(), dt_cb_sy.Rows[0]["I_BENYUESL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl);
            }
            else if (dt_cb.Rows.Count > 0 && dt_cb_sy.Rows.Count < 1)
            {
                var cbzt = dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString() != ""
                    ? dt_cb.Rows[0]["I_CHAOBIAOZT"].ToString()
                    : "0";
                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2,
                    cbzt, dt_cb.Rows[0]["I_BENYUECM"].ToString(), dt_cb.Rows[0]["I_BENYUESL"].ToString(),
                    "0", "0",
                    dt_cb.Rows[0]["I_JIHUASL"].ToString(), dt_cb.Rows[0]["I_SHUILIANGDL"].ToString(),
                    dt_cb.Rows[0]["I_JIHUASLDL"].ToString(), yhh, bl);
            }
            else
            {
                var bycm_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUECM"].ToString() : "0";
                var bysl_sy = dt_cb_sy.Rows.Count > 0 ? dt_cb_sy.Rows[0]["I_BENYUESL"].ToString() : "0";

                msg = string.Join("|", dt.Rows[0]["S_YONGHUM"].ToString(), dt.Rows[0]["N_SHUIJIA"].ToString(), bh, dz,
                    dt.Rows[0]["S_CEHAO"].ToString(), sbh, sllb2, "0", "", "",
                    bycm_sy, bysl_sy,
                    "", "", "", yhh, bl);
            }

            return msg;
        }

        /// <summary>
        ///     水量类别转换
        /// </summary>
        /// <param name="sllb"></param>
        /// <returns></returns>
        private string getSllb(string sllb)
        {
            var sllb2 = "";
            if (sllb == "1" || sllb == "001")
                sllb2 = "普通采";
            else if (sllb == "2" || sllb == "002")
                sllb2 = "采灌采";
            else if (sllb == "3" || sllb == "003")
                sllb2 = "采灌灌";
            else if (sllb == "4" || sllb == "004")
                sllb2 = "矿泉采";
            else if (sllb == "5" || sllb == "005") sllb2 = "专灌";
            return sllb2;
        }

        private string getSllb2(string sllb)
        {
            var sllb2 = "";
            if (sllb == "普通采")
                sllb2 = "1";
            else if (sllb == "采灌采")
                sllb2 = "2";
            else if (sllb == "采灌灌")
                sllb2 = "3";
            else if (sllb == "矿泉采")
                sllb2 = "4";
            else if (sllb == "专灌") sllb2 = "5";
            return sllb2;
        }

        /// <summary>
        ///     总数
        /// </summary>
        private void Loadzs()
        {
            var wtype = 2; // BusinessTool.WType;
            var CH = cbmCeHao.Text == "" ? "001" : cbmCeHao.Text;
            var dt = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, 0, CH).Tables[0];
            var rs = dt.Rows.Count > 0 ? Convert.ToInt32(dt.Rows[0][0]) : 1;
            rows.Value = rs.ToString();
            lbzjl.Text = "共" + rs + "条";
        }

        private void LoadDataTxt(int rn)
        {
            var CH = cbmCeHao.Text == "" ? "001" : cbmCeHao.Text;
            row.Value = rn.ToString();
            var wtype = 2; // BusinessTool.WType;
            var dtSouce = _spHelper.P_QSCB_CHAOBIAOSJ2(wtype, rn, CH).Tables[0];
            if (dtSouce.Rows.Count > 0) SetData(dtSouce);
        }

        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }


        private string GetBL2(string type)
        {
            // 086F34BF-6DF0-4653-955C-5DBE4A7351E0	01	100%水量
            //1814067B-BEDE-4595-920D-E512329C3576	02	1%水量
            //36C72A6F-3DBA-4A82-A33A-3DBB24F57588	03	从电量
            //5ECCF720-63E9-42E0-B28C-4666C506784E	
            /*
             --地热井
                select a.s_yonghuh ,a.s_yonghum,a.S_FEETYPE  
                from qs_yg_yonghuxx a 
                where a.i_wtype=2 and a.i_jlzt=0
                and s_yonghuh in (select s_yonghubh from qs_yh_yonghusjgxb where i_jlzt=0 and  s_jingbianh in
                (select s_jingbh from qs_yh_shenjingxxb where i_jlzt=0 and s_jingbiaosbh=1))
                ---------------------------------------------------------------------------------------------------------
                UPDATE qs_yg_yonghuxx SET S_FEETYPE=02 WHERE S_YONGHUH IN ('5029064','5022122');
                UPDATE qs_yg_yonghuxx SET S_FEETYPE=01 WHERE S_YONGHUH NOT IN ('5029064','5022122') AND I_WTYPE=2 AND S_YONGHUH LIKE '5%'
             */


            var bl = "";
            if (type == "02" || type == "1%水量")
                bl = "1";
            else if (type == "01" || type == "100%水量")
                bl = "100";
            else
                bl = "";

            return bl;
        }

        private string GetBL(string yhh)
        {
            // 086F34BF-6DF0-4653-955C-5DBE4A7351E0	01	100%水量
            //1814067B-BEDE-4595-920D-E512329C3576	02	1%水量
            //36C72A6F-3DBA-4A82-A33A-3DBB24F57588	03	从电量
            //5ECCF720-63E9-42E0-B28C-4666C506784E	
            /*
             --地热井
                select a.s_yonghuh ,a.s_yonghum,a.S_FEETYPE  
                from qs_yg_yonghuxx a 
                where a.i_wtype=2 and a.i_jlzt=0
                and s_yonghuh in (select s_yonghubh from qs_yh_yonghusjgxb where i_jlzt=0 and  s_jingbianh in
                (select s_jingbh from qs_yh_shenjingxxb where i_jlzt=0 and s_jingbiaosbh=1))
                ---------------------------------------------------------------------------------------------------------
                UPDATE qs_yg_yonghuxx SET S_FEETYPE='02' WHERE S_YONGHUH IN ('5029064','5022122');
                UPDATE qs_yg_yonghuxx SET S_FEETYPE='01' WHERE S_YONGHUH NOT IN ('5029064','5022122') AND I_WTYPE=2 AND S_YONGHUH LIKE '5%'
             */

            //DataTable dt = _YHXXBLL.SelectQS_YG_YONGHUXXesDynamic(string.Format("i_jlzt=0 and s_yonghuh='{0}'", yhh), null);
            //string type = dt.Rows.Count > 0 ? dt.Rows[0]["S_FEETYPE"].ToString() : "";
            var bl = "100";
            //if (type == "02" || type == "1%水量")
            //{
            //    bl = "1";
            //}
            //else if (type == "01" || type == "100%水量")
            //{
            //    bl = "100";
            //}
            //else
            //{
            //    bl = "";
            //}

            return bl;
        }


        protected void UpLoadFile_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = "操作失败";
            var userid = LoginInfo.CurrentUser.UserID;
            //e.IsValid = false;
            const string UploadDirectory = "/UPLOAD/DXS/CB/"; //图片上传文件夹路径
            if (!UpLoadFile.IsValid)
            {
                //return string.Empty;
            }
            else
            {
                var fileName = Guid.NewGuid() + Path.GetExtension(UpLoadFile.FileName); //文件名
                var filePath = Path.Combine(Server.MapPath(UploadDirectory), fileName); //文件完整物理路径
                UpLoadFile.SaveAs(filePath);
                var dt = ReadExcel(filePath);

                if (dt != null && dt.Rows.Count > 0)
                {
                    var newdt = _spHelper.P_QSCB_DRSJ(userid, 0).Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        var newdr = newdt.NewRow();
                        if (dt.Columns.Contains("水量年")) newdr["I_Y"] = dr["水量年"];
                        if (dt.Columns.Contains("水量月")) newdr["I_M"] = dr["水量月"];
                        if (dt.Columns.Contains("用户号")) newdr["S_YONGHUH"] = dr["用户号"];
                        if (dt.Columns.Contains("用户名")) newdr["S_YONGHUM"] = dr["用户名"];
                        if (dt.Columns.Contains("井编号")) newdr["S_JINGBH"] = dr["井编号"];
                        if (dt.Columns.Contains("册号")) newdr["S_CEHAO"] = dr["册号"];
                        if (dt.Columns.Contains("水表编号")) newdr["S_SHUIBIAOBH"] = dr["水表编号"];
                        if (dt.Columns.Contains("水量类别")) newdr["S_SHUILIANGLB"] = dr["水量类别"];
                        if (dt.Columns.Contains("抄表状态")) newdr["I_CHAOBIAOZT"] = dr["抄表状态"];
                        if (dt.Columns.Contains("本月抄码")) newdr["I_BENYUECM"] = dr["本月抄码"];
                        if (dt.Columns.Contains("本月水量")) newdr["I_BENYUESL"] = dr["本月水量"];
                        newdt.Rows.Add(newdr);
                    }

                    var tablename = "PLANWATER_TEMP.QSCB_DRSJ";
                    if (BusinessTool.InsertOracelData(newdt, tablename))
                    {
                        //e.CallbackData = "1111";

                        var ds = _spHelper.P_QSCB_DRSJ(userid, 1);
                        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                        {
                            e.IsValid = true;
                            e.CallbackData = ds.Tables[0].Rows[0][0].ToString();
                        }
                    }
                }
            }
        }

        private DataTable ReadExcel(string ExcelFilePath)
        {
            var dt = new DataTable();
            //string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; " +"Data Source=" +ExcelFilePath + ";Extended Properties=Excel 8.0";
            var strConnection =
                "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';data source=" +
                ExcelFilePath;
            var Connection = new OleDbConnection(strConnection);
            Connection.Open();
            var CommandText = "";
            var dtsheetName = GetWorkBookName(Connection);
            if (dtsheetName != null && dtsheetName.Rows.Count > 0)
                CommandText = "select * from [" + dtsheetName.Rows[0]["TABLE_NAME"] + "]";
            else
                return null;
            var comm = new OleDbCommand(CommandText, Connection);

            try
            {
                var da = new OleDbDataAdapter(comm);
                var Ds = new DataSet();
                da.Fill(Ds);
                dt = Ds.Tables[0];
            }
            catch (Exception)
            {
            }
            finally
            {
                if (Connection.State == ConnectionState.Open) Connection.Close();
            }

            return dt;
        }

        /// <summary>
        ///     获取Excel中的所有工作簿
        /// </summary>
        /// <param name="Connection">OleDbConnection对象</param>
        /// <returns></returns>
        public static DataTable GetWorkBookName(OleDbConnection Connection)
        {
            DataTable Dt = null;
            try
            {
                if (Connection.State == ConnectionState.Closed) Connection.Open();
                Dt = Connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (Connection.State == ConnectionState.Open) Connection.Close();
            }

            return Dt;
        }

        #endregion
    }
}